package com.arcaneshift.model.action.game;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.arcaneshift.model.util.RoundPhase;

public class Round implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private List<BattlePlayer> battlePlayers;
	private int current;
	
	@Deprecated
	private Player currentPlayer;
	@Deprecated
	private RoundPhase currentPhase;
	
	public Round() {
		battlePlayers = new ArrayList<BattlePlayer>();
	}
	
	public void addBattlePlayer(BattlePlayer battlePlayer) {
		battlePlayer.setPlayerNumber(battlePlayers.size());
		battlePlayers.add(battlePlayer);
	}
	
	public BattlePlayer getCurrentBattlePlayer() {
		if (battlePlayers.size() > current) {
			return battlePlayers.get(current);
		}
		return null;
	}
	
	public void nextBattlePlayer() {
		if (++current >= battlePlayers.size()) {
			current = 0;
		}
	}
	
	public void setBattlePlayerReady(int index) {
		if (index < battlePlayers.size()) {
			battlePlayers.get(index).setReady(true);
		}
	}
	
	/**
	 * Test if all the players are ready.
	 */
	public boolean areBattlePlayersReady() {
		for (int i = 0; i < battlePlayers.size(); i++) {
			if (!battlePlayers.get(i).getReady()) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * Check if the player with the provided userId is in this game.
	 */
	public boolean containsId(Long userId) {
		for (int i = 0; i < battlePlayers.size(); i++) {
			if (battlePlayers.get(i).getPlayer().getSession().getUserId().equals(userId)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Get a list with all the battle players from this game.
	 */
	public List<BattlePlayer> getBattlePlayers() {
		return battlePlayers;
	}
	
	@Deprecated
	public void setCurrentPlayer(Player player) {
		this.currentPlayer = player;
	}
	
	@Deprecated
	public Player getCurrentPlayer() {
		return this.currentPlayer;
	}

	@Deprecated
	public RoundPhase getCurrentPhase() {
		return currentPhase;
	}

	@Deprecated
	public void setCurrentPhase(RoundPhase currentPhase) {
		this.currentPhase = currentPhase;
	}
}
